<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
  <title></title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <style type="text/css">
td.linenos { background-color: #f0f0f0; padding-right: 10px; }
span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }
pre { line-height: 125%; }
body .hll { background-color: #ffffcc }
body  { background: #ffffff; }
body .c { color: #008000 } /* Comment */
body .err { border: 1px solid #FF0000 } /* Error */
body .k { color: #0000ff } /* Keyword */
body .cm { color: #008000 } /* Comment.Multiline */
body .cp { color: #0000ff } /* Comment.Preproc */
body .c1 { color: #008000 } /* Comment.Single */
body .cs { color: #008000 } /* Comment.Special */
body .ge { font-style: italic } /* Generic.Emph */
body .gh { font-weight: bold } /* Generic.Heading */
body .gp { font-weight: bold } /* Generic.Prompt */
body .gs { font-weight: bold } /* Generic.Strong */
body .gu { font-weight: bold } /* Generic.Subheading */
body .kc { color: #0000ff } /* Keyword.Constant */
body .kd { color: #0000ff } /* Keyword.Declaration */
body .kn { color: #0000ff } /* Keyword.Namespace */
body .kp { color: #0000ff } /* Keyword.Pseudo */
body .kr { color: #0000ff } /* Keyword.Reserved */
body .kt { color: #2b91af } /* Keyword.Type */
body .s { color: #a31515 } /* Literal.String */
body .nc { color: #2b91af } /* Name.Class */
body .ow { color: #0000ff } /* Operator.Word */
body .sb { color: #a31515 } /* Literal.String.Backtick */
body .sc { color: #a31515 } /* Literal.String.Char */
body .sd { color: #a31515 } /* Literal.String.Doc */
body .s2 { color: #a31515 } /* Literal.String.Double */
body .se { color: #a31515 } /* Literal.String.Escape */
body .sh { color: #a31515 } /* Literal.String.Heredoc */
body .si { color: #a31515 } /* Literal.String.Interpol */
body .sx { color: #a31515 } /* Literal.String.Other */
body .sr { color: #a31515 } /* Literal.String.Regex */
body .s1 { color: #a31515 } /* Literal.String.Single */
body .ss { color: #a31515 } /* Literal.String.Symbol */

  </style>
</head>
<body>
<h2></h2>

<div class="highlight"><pre><span class="c">#!/usr/bin/python</span>
<span class="c">#</span>
<span class="c"># This example shows how to use find_candidate_object_locations().  The</span>
<span class="c"># function takes an input image and generates a set of candidate rectangles</span>
<span class="c"># which are expected to bound any objects in the image.</span>
<span class="c"># It is based on the paper:</span>
<span class="c">#    Segmentation as Selective Search for Object Recognition by Koen E. A. van de Sande, et al.</span>
<span class="c">#</span>
<span class="c"># Typically, you would use this as part of an object detection pipeline.</span>
<span class="c"># find_candidate_object_locations() nominates boxes that might contain an</span>
<span class="c"># object and you then run some expensive classifier on each one and throw away</span>
<span class="c"># the false alarms.  Since find_candidate_object_locations() will only generate</span>
<span class="c"># a few thousand rectangles it is much faster than scanning all possible</span>
<span class="c"># rectangles inside an image.</span>
<span class="c">#</span>
<span class="c">#</span>
<span class="c"># COMPILING/INSTALLING THE DLIB PYTHON INTERFACE</span>
<span class="c">#   You can install dlib using the command:</span>
<span class="c">#       pip install dlib</span>
<span class="c">#</span>
<span class="c">#   Alternatively, if you want to compile dlib yourself then go into the dlib</span>
<span class="c">#   root folder and run:</span>
<span class="c">#       python setup.py install</span>
<span class="c">#   or</span>
<span class="c">#       python setup.py install --yes USE_AVX_INSTRUCTIONS</span>
<span class="c">#   if you have a CPU that supports AVX instructions, since this makes some</span>
<span class="c">#   things run faster.  </span>
<span class="c">#</span>
<span class="c">#   Compiling dlib should work on any operating system so long as you have</span>
<span class="c">#   CMake and boost-python installed.  On Ubuntu, this can be done easily by</span>
<span class="c">#   running the command:</span>
<span class="c">#       sudo apt-get install libboost-python-dev cmake</span>
<span class="c">#</span>
<span class="c">#   Also note that this example requires scikit-image which can be installed</span>
<span class="c">#   via the command:</span>
<span class="c">#       pip install scikit-image</span>
<span class="c">#   Or downloaded from http://scikit-image.org/download.html. </span>



<span class="kn">import</span> <span class="nn">dlib</span>
<span class="kn">from</span> <span class="nn">skimage</span> <span class="kn">import</span> <span class="n">io</span>

<span class="n">image_file</span> <span class="o">=</span> <span class="s">&#39;../examples/faces/2009_004587.jpg&#39;</span>
<span class="n">img</span> <span class="o">=</span> <span class="n">io</span><span class="o">.</span><span class="n">imread</span><span class="p">(</span><span class="n">image_file</span><span class="p">)</span>

<span class="c"># Locations of candidate objects will be saved into rects</span>
<span class="n">rects</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">dlib</span><span class="o">.</span><span class="n">find_candidate_object_locations</span><span class="p">(</span><span class="n">img</span><span class="p">,</span> <span class="n">rects</span><span class="p">,</span> <span class="n">min_size</span><span class="o">=</span><span class="mi">500</span><span class="p">)</span>

<span class="k">print</span><span class="p">(</span><span class="s">&quot;number of rectangles found {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">rects</span><span class="p">)))</span> 
<span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">d</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">rects</span><span class="p">):</span>
    <span class="k">print</span><span class="p">(</span><span class="s">&quot;Detection {}: Left: {} Top: {} Right: {} Bottom: {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
        <span class="n">k</span><span class="p">,</span> <span class="n">d</span><span class="o">.</span><span class="n">left</span><span class="p">(),</span> <span class="n">d</span><span class="o">.</span><span class="n">top</span><span class="p">(),</span> <span class="n">d</span><span class="o">.</span><span class="n">right</span><span class="p">(),</span> <span class="n">d</span><span class="o">.</span><span class="n">bottom</span><span class="p">()))</span>
</pre></div>
</body>
</html>
